package com.hds.modules.app.entity.wx.utils;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.hds.modules.app.entity.wx.common.Common;

import net.sf.json.JSONObject;

public class JssdkConfig {
    public static void main(String[] args) {
        try {
            //jsSDK_Sign("http://mp.weixin.qq.com?params=value#main");
            String ss="http://mp.weixin.qq.com?params=value#main";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @Description: 前端jssdk页面配置需要用到的配置参数
     * @param @return hashmap {appid,timestamp,nonceStr,signature}
     */
  /*  public static HashMap<String, String> jsSDK_Sign(String url) throws Exception {
        String nonce_str = create_nonce_str();

        JSONObject ticketJson = WxTask.getTicket();
        String timestamp= String.valueOf(new Date().getTime());
        String jsapi_ticket = ticketJson.getString("ticket");

        // 注意这里参数名必须全部小写，且必须有序
        String  string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str
                + "&timestamp=" + timestamp  + "&url=" + url;
        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();
        crypt.update(string1.getBytes("UTF-8"));
        String signature = byteToHex(crypt.digest());
        HashMap<String, String> jssdk=new HashMap<String, String>();
        jssdk.put("appId", Common.APPID);
        jssdk.put("timestamp", timestamp);
        jssdk.put("nonceStr", nonce_str);
        jssdk.put("signature", signature);
        System.out.println("map:"+jssdk);
        return jssdk;

    }*/

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    private static String create_nonce_str() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return uuid;
    }

}